package cn.wch.ch934xlib.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;

import cn.wch.ch934xlib.callback.IUsbStateChange;
import cn.wch.ch934xlib.constant.Constant;


public class UsbReceiver extends BroadcastReceiver {

    private IUsbStateChange listener;
    public UsbReceiver() {
        super();
    }

    public void setStateListener(IUsbStateChange listener){
        this.listener=listener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action=intent.getAction();
        if(UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)){
            if(listener!=null){
                listener.usbDeviceAttach((UsbDevice)intent.getParcelableExtra("device"));
            }
        }else if(UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)){
            if(listener!=null){
                listener.usbDeviceDetach((UsbDevice)intent.getParcelableExtra("device"));
            }
        }else if(Constant.REQUEST_PERMISSION_STRING.equals(action)){
            if(listener!=null){
                listener.usbDevicePermission((UsbDevice)intent.getParcelableExtra("device"),
                        intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false));
            }
        }
    }

}
